<template>
	<view class="service">
		<z-paging ref="paging" v-model="dataList" use-chat-record-mode safe-area-inset-bottom bottom-bg-color="#fff"
			@query="queryList">
			<view class="list">
				<template v-for="(item, index) in dataList">
					<view v-if="item.reply" class="item" style="transform: scaleY(-1)">
						<image src="@/static/image/kftouxiang.png"></image>
						<view class="right">
							<view class="text">{{ item.reply }}</view>
							<view class="time">{{ item.reply_time}}</view>
						</view>
					</view>
					<view v-if="item.content" class="item" style="transform: scaleY(-1)">
						<image :src="userInfo.avatar"></image>
						<view class="right">
							<view class="text">{{ item.content }}</view>
							<view class="time">{{ item.create_time}}</view>
						</view>
					</view>
				</template>
			</view>
			<template #bottom>
				<view class="bottom">
					<u--textarea v-model="text" placeholder="请输入咨询内容" :adjustPosition="false" autoHeight></u--textarea>
					<view class="button">
						<up-button type="primary" :customStyle="customStyle" text="发送" @click="handleSend"></up-button>
					</view>
				</view>
			</template>
		</z-paging>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import user from '@/api/user.js'
	import dayjs from 'dayjs'
	const text = ref('')
	const userInfo = uni.getStorageSync('userInfo')
	const dataList = ref([])
	const paging = ref()
	const count = ref(0)
	const queryList = async (pageNo, pageSize) => {
		const {
			status,
			info,
			data
		} = await user.serviceMessage({
			page: pageNo,
			page_size: pageSize
		})
		if (status == 1) {
			paging.value.complete(data.list)
			count.value = data.count
		}
	}
	const handleSend = async () => {
		const {
			status,
			info,
			data
		} = await user.putMessage({
			content: text.value
		})
		if (status == 1) {
			console.log(dataList.value);
			if (count.value == 0) {
				paging.value.complete([{
					content: text.value,
					create_time: dayjs(new Date()).format('MM月DD日 HH:mm')
				}])
				count.value++
			} else {
				dataList.value.unshift({
					content: text.value,
					create_time: dayjs(new Date()).format('MM月DD日 HH:mm')
				})
			}
			text.value = ''
		}
	}
	const customStyle = {
		height: '76rpx',
		borderRadius: '10rpx !important'
	}
</script>

<style scoped lang="scss">
	.list {
		padding: 40rpx;
		box-sizing: border-box;

		.item {
			display: flex;

			image {
				width: 90rpx;
				height: 90rpx;
				border-radius: 50%;
				margin-right: 20rpx;
			}

			.right {
				width: calc(100% - 110rpx);

				.text {
					padding: 16rpx 20rpx;
					background: #FFFFFF;
					border-radius: 12rpx;
					border: 2rpx solid #F6F6F6;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #000000;
					line-height: 42rpx;
					width: fit-content;
					word-break: break-all;
				}

				.time {
					margin-top: 10rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #D5D0D0;
					line-height: 28rpx;
				}
			}
		}

		.item+.item {
			margin-top: 40rpx;
		}
	}

	.bottom {
		background: #FFFFFF;
		box-shadow: 0rpx -8rpx 16rpx 0rpx rgba(128, 128, 128, 0.06);
		width: 100%;
		display: flex;
		padding: 14rpx 20rpx;
		box-sizing: border-box;

		.button {
			margin-left: 20rpx;
			width: 154rpx;
		}
	}
</style>